"""
104. 货仓选址
https://www.acwing.com/problem/content/106/
@date: 2021年1月10日15:44
@author: mizzle rain
"""
n = int(input().strip())
locations = list(map(int, input().strip().split()))
locations.sort()
res = 0
for item in locations:
    res += abs(item-locations[n >> 1])
print(res)

